import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:status_bar_control/status_bar_control.dart';
import 'package:FiduMart/app_routes.dart';
import 'package:FiduMart/constants/constants.dart';
import 'package:FiduMart/core/utils/crypto_util.dart';
import 'package:FiduMart/core/utils/stringUtil.dart';

import 'pref_utils.dart';

class CommonUtil {
//把这个时间戳转换成2020-12-12的格式

  static String timestampToDate(int timestamp, {String format = "yyyy-MM-dd"}) {
    var date = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
    var formatter = DateFormat(format);
    String formattedDate = formatter.format(date);
    return formattedDate;
  }

  static String timestampToDateMill(int timestamp, {String format = "yyyy-MM-dd HH:mm:ss"}) {
    var date = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
    var formatter = DateFormat(format);
    String formattedDate = formatter.format(date);
    return formattedDate;
  }

  //切换横屏
  static void changeLandscape() {
    StatusBarControl.setHidden(true, animation:StatusBarAnimation.SLIDE);
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeRight,
      DeviceOrientation.landscapeLeft,
    ]);
  }

  //切换竖屏
  static void changePortrait() {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);
  }

  static void showErrorToast(String message) {
    Fluttertoast.showToast(
      msg: "${message.split(":").last}".trim().tr,
      toastLength: Toast.LENGTH_SHORT,
      gravity: ToastGravity.CENTER,
      timeInSecForIosWeb: 1,
      backgroundColor: Colors.red,
      textColor: Colors.white,
      fontSize: 16.0,
    );
  }

//  默认host https://h5-club.umcrypto.io/" : "https://120-union-h5.utonmos.com/"
//   url: /business/invite
  static Future<void> openWeb(String url,String title,{String? hostUrl,String? isGame}) async {
    if(!StringUtil.isEmpty(isGame) && isGame == "1"){
      await StatusBarControl.setHidden(true, animation:StatusBarAnimation.SLIDE);
    }

    if (hostUrl == null) {
      hostUrl = getHtmlAddress();
    }
    final pref = Get.find<PrefUtils>();

    var langStr = await pref.getLang();
    Map<String, String> actLangMap = {
      'en_US': 'en',
      'ru_RU': 'ru',
      'th_TH': 'tha',
      'zh_CN': 'zh',
    };
    var langStrh5 = actLangMap[langStr] ?? 'en';
    var tokenStr = await pref.getToken();

    var cacheRefresh = await pref.getWebCacheVersion();
    //判断url是否有问号
    if (url.contains("?")) {
      url = url + "&";
    } else {
      url = url + "?";
    }
    var wbh5 = hostUrl+"${url}token=$tokenStr&tag=APP&lang=$langStrh5&cacheVersion=$cacheRefresh";

    //去充值
    Get.toNamed(AppRoutes.webviewScreen, parameters: {
      "url": wbh5,
      "title": title,
      "isGame": isGame ?? "0"
    });
  }

  //打开其他app
  static Future<void> openOtherApp(String packageName,String url,String title) async {
    // if (await canLaunchUrl(Uri.parse(url))) {
    //   Logger.log("canLaunchUrl true");
    //   await launchUrl(Uri.parse(url));
    // } else {
    //   Logger.log("canLaunchUrl false");
      openWeb(url, title,hostUrl: "");
    // }
  }

  //获取加密code
  static String getEncryptCode(String millsconds){
    //时间戳+9527+时间戳前六位
    var timestampStr6 = millsconds.substring(0,6);
    var originCode = "${millsconds}9527$timestampStr6";
    return CryptoUtil.generateMd5(originCode);
  }
}